package com.hildeberto.versions.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;

public enum Repository {

    FACTORY;

    public <T> T unmarshal(Class<T> type, String xmlContent) {
        try {
            byte[] bXmlContent = xmlContent.getBytes();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bXmlContent);

            JAXBContext jaxbContext = JAXBContext.newInstance(type);

            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            return (T) unmarshaller.unmarshal(inputStream);
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }
}
